package com.framework.modules.base.service.impl;

import com.framework.common.utils.DateUtils;
import com.framework.modules.base.entity.UserDoingEntity;
import com.framework.modules.base.service.UserDoingService;
import com.framework.modules.chat.entity.ChatFriendEntity;
import com.framework.modules.chat.service.ChatFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.UserSpaceDao;
import com.framework.modules.base.entity.UserSpaceEntity;
import com.framework.modules.base.service.UserSpaceService;
import org.springframework.transaction.annotation.Transactional;


@Service("userSpaceService")
public class UserSpaceServiceImpl extends ServiceImpl<UserSpaceDao, UserSpaceEntity> implements UserSpaceService {

    @Autowired
    private UserDoingService userDoingService;

    @Autowired
    private ChatFriendService chatFriendService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserSpaceEntity> page = this.selectPage(
                new Query<UserSpaceEntity>(params).getPage(),
                new EntityWrapper<UserSpaceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer updateAttentCount(Integer id,Integer likeCount,String type) {
        UserSpaceEntity userSpaceEntity=baseMapper.selectById(id);
        if(userSpaceEntity==null&&"add".equals(type)){
            userSpaceEntity=new UserSpaceEntity();
            userSpaceEntity.setUid(id);
            userSpaceEntity.setAttentCount(1);
            userSpaceEntity.setFansCount(0);
            userSpaceEntity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            return this.baseMapper.insert(userSpaceEntity);
        }
        if("add".equals(type)){
            return this.baseMapper.addLikeCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reduceLikeCount(id);
        }else{
            return this.baseMapper.updateLikeCount(id,likeCount);
        }
    }

    @Override
    public Integer updateFansCount(Integer id,Integer fansCount,String type) {
        UserSpaceEntity userSpaceEntity=baseMapper.selectById(id);
        if(userSpaceEntity==null&&"add".equals(type)){
            userSpaceEntity=new UserSpaceEntity();
            userSpaceEntity.setUid(id);
            userSpaceEntity.setAttentCount(0);
            userSpaceEntity.setFansCount(1);
            userSpaceEntity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            return baseMapper.insert(userSpaceEntity);
        }
        if("add".equals(type)){
            return this.baseMapper.addFansCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reduceFansCount(id);
        }else{
            return this.baseMapper.updateFansCount(id,fansCount);
        }
    }

    @Override
    @Transactional
    public Object likeUser(UserDoingEntity entity) {
        Integer objectId=entity.getObjectId();
        Integer uid=Integer.valueOf(entity.getUid());
        int count=chatFriendService.selectCount(new EntityWrapper<ChatFriendEntity>()
                .eq("uid",entity.getUid()).eq("target_uid",entity.getObjectId()).eq("is_liked",1));

        List<ChatFriendEntity> chatFriendEntities=new ArrayList<>(2);
        Integer time=DateUtils.getSecondTimestamp(new Date());
        //from
        ChatFriendEntity chatFriendEntity1=new ChatFriendEntity();
        chatFriendEntity1.setUid(uid);
        chatFriendEntity1.setTargetUid(objectId);
        chatFriendEntity1.setLastUpdate(time);

        List<UserSpaceEntity> userSpaceEntities=new ArrayList<>(2);
        UserSpaceEntity userSpaceEntity1=new UserSpaceEntity();
        userSpaceEntity1.setUid(uid);
        userSpaceEntity1.setAdddate(time);

        //to
        ChatFriendEntity chatFriendEntity2=new ChatFriendEntity();
        chatFriendEntity2.setUid(objectId);
        chatFriendEntity2.setTargetUid(uid);
        chatFriendEntity2.setBlockDate(time);
        chatFriendEntity2.setLastUpdate(time);

        UserSpaceEntity userSpaceEntity2=new UserSpaceEntity();
        userSpaceEntity2.setUid(objectId);
        userSpaceEntity2.setAdddate(time);

        boolean isExist=count>0;
        //判断是否喜欢过
        if(isExist){
            //取消喜欢
            entity.setType("cancel_like");
            //关注状态 1=喜欢 0=不喜欢
            chatFriendEntity1.setIsLiked(0);

            userSpaceEntity1.setLikeCount(-1);

            chatFriendEntity2.setBlockMessage("他（她）不喜欢你了");
            chatFriendEntity2.setBlockCount(1);
        }else{
            //关注状态 1=喜欢 0=不喜欢
            chatFriendEntity1.setIsLiked(1);

            userSpaceEntity1.setLikeCount(1);

            chatFriendEntity2.setBlockMessage("他（她）喜欢了你");
            chatFriendEntity2.setBlockCount(1);
        }
        //更新状态
        chatFriendEntities.add(chatFriendEntity1);
        chatFriendEntities.add(chatFriendEntity2);
        chatFriendService.saveOrUpdateBatch(chatFriendEntities);
        //更新用户喜欢数
        userSpaceEntities.add(userSpaceEntity1);
        userSpaceEntities.add(userSpaceEntity2);
        this.baseMapper.saveOrUpdateBatch(userSpaceEntities);
        //添加动作
        entity.setAdddate(time);
        userDoingService.addDoing(entity);
        return !isExist;
    }

    @Override
    @Transactional
    public Object attentUser(UserDoingEntity entity) {
        Integer objectId=entity.getObjectId();
        Integer uid=Integer.valueOf(entity.getUid());

        List<ChatFriendEntity> chatFriendEntities=new ArrayList<>(2);
        int count=chatFriendService.selectCount(new EntityWrapper<ChatFriendEntity>()
                .eq("uid",entity.getUid()).eq("target_uid",entity.getObjectId()).eq("state",1));

        boolean isExist=count>0;
        Integer time=DateUtils.getSecondTimestamp(new Date());
        // from先生成两条好友信息
        ChatFriendEntity chatFriendEntity1=new ChatFriendEntity();
        chatFriendEntity1.setUid(uid);
        chatFriendEntity1.setTargetUid(objectId);
        chatFriendEntity1.setLastUpdate(time);

        List<UserSpaceEntity> userSpaceEntities=new ArrayList<>(2);
        UserSpaceEntity userSpaceEntity1=new UserSpaceEntity();
        userSpaceEntity1.setUid(Integer.parseInt(entity.getUid()));
        userSpaceEntity1.setAdddate(time);

        //to
        ChatFriendEntity chatFriendEntity2=new ChatFriendEntity();
        chatFriendEntity2.setTargetUid(uid);
        chatFriendEntity2.setUid(objectId);
        chatFriendEntity2.setBlockDate(time);
        chatFriendEntity2.setLastUpdate(time);

        UserSpaceEntity userSpaceEntity2=new UserSpaceEntity();
        userSpaceEntity2.setUid(Integer.parseInt(entity.getObjectUid()));
        userSpaceEntity2.setAdddate(time);

        //判断是否关注过
        if(isExist){
            //取消关注
            entity.setType("cancel_attent");
            //关注状态 1=关注 0=不关注
            chatFriendEntity1.setState(0);
            //from减少一个关注
            userSpaceEntity1.setAttentCount(-1);
            //to减少一个粉丝
            userSpaceEntity2.setFansCount(-1);
            //添加消息
            chatFriendEntity2.setBlockCount(1);
            chatFriendEntity2.setBlockMessage("他（她）取消关注你了");
        }else{
            //关注状态 1=关注 0=不关注
            chatFriendEntity1.setState(1);

            //from增加一个关注
            userSpaceEntity1.setAttentCount(1);
            //to增加一个粉丝
            userSpaceEntity2.setFansCount(1);

            chatFriendEntity2.setBlockCount(1);
            chatFriendEntity2.setBlockMessage("他（她）关注你了");
        }
        chatFriendEntities.add(chatFriendEntity1);
        chatFriendEntities.add(chatFriendEntity2);
        chatFriendService.saveOrUpdateBatch(chatFriendEntities);

        //更新用户关注数和粉丝数
        userSpaceEntities.add(userSpaceEntity1);
        userSpaceEntities.add(userSpaceEntity2);
        this.baseMapper.saveOrUpdateBatch(userSpaceEntities);
        //添加动作
        entity.setAdddate(time);
        userDoingService.addDoing(entity);
        return !isExist;
    }

    @Override
    public Object updateUserSpace(UserSpaceEntity entity) {
        return this.baseMapper.updateById(entity);
    }

}
